package com.ruoyi.generator.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.generator.entity.*;
import com.ruoyi.generator.entity.dto.EditInvoice;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/invoice")
public class InvoiceController extends BaseController {

    @Autowired
    private com.ruoyi.generator.service.InvoiceService invoiceService;

    @GetMapping("/list")
    public TableDataInfo list(InvoiceSearchCriteria c )
    {
        startPage();
        List<InvoiceForm> list = invoiceService.selectInvoiceList(c);
        return getDataTable(list);
    }

    /**
     * 查询可开票金额
     */
    @GetMapping("/queryAvailableAmountByInvoiceId")
    public AjaxResult queryAvailableAmountByInvoiceId(@RequestParam String invoiceId) {
        try {
            String availableAmount = invoiceService.queryAvailableAmountByInvoiceId(invoiceId);
            return AjaxResult.success(availableAmount);
//            // 调用服务层方法查询可开票金额
//            Double availableAmount = invoiceService.queryAvailableAmountByInvoiceId(invoiceId);
//            return AjaxResult.success(availableAmount);
        } catch (Exception e) {
            return AjaxResult.error("查询可开票金额失败");
        }
    }
    /**
     * 修改发票金额
     */
    @PostMapping("/editInvoiceAmount")
    public int  editInvoiceAmount(@RequestBody EditInvoice editInvoice) {
        try {
            System.out.println("invoiceForm="+editInvoice);
            // 调用服务层方法更新发票金额
            int result =invoiceService.editInvoiceAmount(editInvoice);
            return result;
        } catch (Exception e) {

            System.out.println("修改发票金额失败");
            System.out.println("异常信息："+e.getMessage());
            return 500;
        }
    }


}